package com.hq.base.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hq.base.consts.Const;
import com.hq.base.entity.ApiLog;
import com.hq.base.entity.ApiNews;
import com.hq.base.enums.NewsTypeEnum;
import com.hq.base.properties.ApplicationProperties;
import com.hq.base.service.ApiLogService;
import com.hq.base.service.ApiNewsService;
import com.hq.base.vo.NewsApiResultVO;
import com.hq.base.vo.NewsApiVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component
@Slf4j
public class NewsTask {

    @Resource
    private ApiLogService apiLogService;
    @Resource
    private ApiNewsService apiNewsService;

//    @Scheduled(cron = "0/5 * * * * ?")
    public void execute() {
        List<ApiNews> apiNewsList = new ArrayList<>();
        ApiLog apiLog = apiLogService.queryLastLog(ApplicationProperties.NEWS_API);
        Integer number = ObjectUtil.isNull(apiLog) ? 0 : apiLog.getNumber()+1;
        Integer type = ObjectUtil.isNull(apiLog) ? 1 : apiLog.getType() ;
        Integer pageNo = ObjectUtil.isNull(apiLog) ? 1 : apiLog.getPageNo() + 1;
        if (number <= Const.MAX_REQUEST_COUNT) {
            if (pageNo>Const.MAX_PAGE_NO){
                pageNo =1;
                type= type+1;


            }
            Map<String, Object> params = new HashMap<>();
            params.put("type", NewsTypeEnum.getType(type));
            params.put("page", pageNo);
            params.put("page_size", 30);
            params.put("key", ApplicationProperties.APPID);

            String result = HttpUtil.get(ApplicationProperties.NEWS_API, params);
            log.info(result);
            JSONObject jsonObject = JSONUtil.parseObj(result);
            NewsApiVO newsApiVO = jsonObject.toBean(NewsApiVO.class);
            String reason = newsApiVO.getReason();
            if (Const.reason.equals(reason)||reason.equals("success")){
                NewsApiResultVO apiResult = newsApiVO.getResult();
                List<JSONObject> data = apiResult.getData();
                data.forEach(row->{
                    Date date = row.getDate("date");
                    String author_name = row.getStr("author_name");
                    Integer isContent = row.getInt("is_content");
                    String thumbnail_pic_s = row.getStr("thumbnail_pic_s");
                    String uniquekey = row.getStr("uniquekey");
                    String title = row.getStr("title");
                    String url = row.getStr("url");
                    String category = row.getStr("category");
                    ApiNews apiNews = ApiNews.builder().id(uniquekey).title(title)
                            .category(category).newsUrl(url)
                            .thumbnailPic(thumbnail_pic_s)
                            .isContent(isContent)
                            .authorName(author_name)
                            .date(date).build();
                    apiNewsList.add(apiNews);
                });


                ApiLog newLog = ApiLog.builder().apiUrl(ApplicationProperties.NEWS_API)
                        .type(type)
                        .date(Const.NOW_YMD).pageNo(pageNo).number(number).build();
                apiLogService.save(newLog);
            }
        }

        if (CollUtil.isNotEmpty(apiNewsList)){
            apiNewsService.saveOrUpdateBatch(apiNewsList);
        }
    }



}
